﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Example.Site.Controllers.Api;
using Moq;
using Example.Base.Interfaces.Services.DataServices;
using Example.Base.Entities;
using System.Threading;
using System.Threading.Tasks;
using Example.Data.Services.DataServices;
using Example.Site.Models;
using System.Net.Http;
using System.Web.Http.Hosting;
using System.Web.Http;

namespace Site.Test
{
    [TestClass]
    public class DataControllerTest
    {
        DataController controller;
        Mock<IDataService<IEnumerable<Tweet>>> tweetServiceMock;
        Mock<IDataService<Region>> weatherServiceMock;

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize()]
        public void MyTestInitialize() 
        {
            controller = new DataController();
            tweetServiceMock = new Mock<IDataService<IEnumerable<Tweet>>>();
            weatherServiceMock = new Mock<IDataService<Region>>();

            TwitterServiceExtension.AsyncGetFactory = 
                (dataservice, query) => {
                    return Task.Factory.StartNew<IEnumerable<Tweet>>(() =>
                        {
                            Thread.Sleep(5000);
                            return new Mock<IEnumerable<Tweet>>().Object;
                        });
                };

            weatherServiceMock
                .Setup(x => x.AsyncGet())
                .Returns(() =>
                    {
                        return Task.Factory.StartNew<Region>(() =>
                        {
                            Thread.Sleep(5000);
                            return new Region();
                        });
                    });

            controller.TwitterService = tweetServiceMock.Object;
            controller.WeatherService = weatherServiceMock.Object;
        }

        [TestMethod]
        public void GetTest()
        {
            controller.Request = new HttpRequestMessage();
            controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

            var startTime = DateTime.Now;
            controller.Get(new GetModel { Query = It.IsAny<string>() });            
            var endTime = DateTime.Now;

            var delta = endTime - startTime;

            TestContext.WriteLine("{0}: Delta = {1}", DateTime.Now, delta);

            if (delta.Seconds > 10)
                Assert.Fail();           
        }
    }
}
